import { _decorator, CCFloat, CCInteger, Component, geometry, MeshRenderer, Node, PhysicsSystem, Vec3 } from 'cc';
import { PHY_GROUP } from '../../PhyCustom/RigidBodyMaskCustom';
import { GameItemID } from '../GameItemID/GameItemID';
import { NailColorType } from '../CustomModel';
const { ccclass, property } = _decorator;

export interface NailInitData {
    color: NailColorType;
}

/**
 * 固定物基类
 * 行为: 
 *      1. 检测上方是否有遮挡物, 上方0.11米内
 *      2. 拔出
 *      3. 不能拔出 
 */
@ccclass('NailBase')
export class NailBase extends Component {

    @property({
        type: Node,
        tooltip: "射线检测起点"
    })
    originNode: Node = null

    @property({
        type: CCFloat,
        tooltip: "射线检测距离"
    })
    rayCistance: number = 0.11


    @property({
        type: [MeshRenderer],
        tooltip: "钉子模型"
    })
    models: MeshRenderer[] = []


    public get itemID(): number {
        return this._itemID
    }
    private set itemID(id: number) {
        this._itemID = id
    }
    private _itemID: number = -1

    protected _data: NailInitData

    /**
     *  可以拔出时
     *  Y轴朝向范围内, 无遮挡物
     * @param click_node_: 当前点击节点
     */
    public onCanPull: (click_node_: Node, itemID_: number) => void

    /**
    *   不可以拔出时
    *   Y轴朝向范围内, 有遮挡物
    * @param block_node_ :
    */
    public onCannotPull: (block_node_: Node) => void

    public init(data_: NailInitData) {
        this._data = data_;
        this.initData()
        this.changeMaterial(this._data.color)
    }

    private initData() {
        this.itemID = GameItemID.generateNailID()
    }

    public click() {
        this.raycast()
    }

    /**
     * 获取射线检测方向
     */
    public getRaycastDirection(): Vec3 {
        return this.originNode.up.clone()
    }

    /**
     * 射线检测
     * 方向: Y轴正上方
     * 掩码: 检测BOARD, 
     * 距离: 0.11(钉子长度)
     */
    protected raycast() {
        const direction_v3 = this.getRaycastDirection()
        const originWorld_v3 = this.originNode.worldPosition.clone()
        const ray = geometry.Ray.create(
            originWorld_v3.x,
            originWorld_v3.y,
            originWorld_v3.z,
            direction_v3.x,
            direction_v3.y,
            direction_v3.z
        )
        const mask = PHY_GROUP.GAME_BOARD
        if (PhysicsSystem.instance.raycastClosest(ray, mask, this.rayCistance)) {
            const block_node = PhysicsSystem.instance.raycastClosestResult.collider.node
            this.cannotPull(block_node)
            if (this.onCannotPull) {
                this.onCannotPull(block_node)
            }
        } else {
            this.canPull()
        }
    }

    /**
     * 改变材质(切换颜色)
     * @param color 
     */
    protected changeMaterial(color: NailColorType) {
        this.models.forEach((meshRenderer_: MeshRenderer) => {
            const material = meshRenderer_.getSharedMaterial(color)
            meshRenderer_.setSharedMaterial(material, 0)
        })
    }

    /**
     * 可以拔出
     */
    protected canPull() {
        if (this.onCanPull) {
            this.onCanPull(this.node, this.itemID)
        }
    }

    /**
     * 不可以拔出
     * @param block_node_ 遮挡物节点
     */
    protected cannotPull(block_node_: Node) {

    }

}


